import { cloneDeep, lodashMap as map, template as lodashTemplate } from "lodash-es";
import { failres, okres } from "../../config/resUtil.js";

import { Controller, App, Post, Body } from "@midwayjs/core";
import { Application } from "../../interface.js";

// 一些默认的公共的操作
@Controller("/erp_cyberstar")
export class UserController {
  @App("koa")
  app: Application;

  @Post("/apis")
  async apis(@Body() body) {
    var funname = body?._name;
    var func = this?.[funname];
    if (!func) {
      return failres(funname + "404");
    }

    var _params = cloneDeep(body);
    delete _params._name;
    delete _params["模块"];

    var _func = func.bind(this);
    return _func(_params);
  }


  async querybuildcomon(query, body, ispage = true) {
    
        if (body?.['id']) {
        query.andWhere("id", body?.['id']);
      }
      
        if (body?.['name']) {
        query.andWhere("name", body?.['name']);
      }
      
        if (body?.['star_acc_id']) {
        query.andWhere("star_acc_id", body?.['star_acc_id']);
      }
      
        if (body?.['platform']) {
        query.andWhere("platform", body?.['platform']);
      }
      
        if (body?.['platform_id']) {
        query.andWhere("platform_id", body?.['platform_id']);
      }
      
        if (body?.['cyberstar_type']) {
        query.andWhere("cyberstar_type", body?.['cyberstar_type']);
      }
      
        if (body?.['agent_user']) {
        query.andWhere("agent_user", body?.['agent_user']);
      }
      
        if (body?.['agent_user_id']) {
        query.andWhere("agent_user_id", body?.['agent_user_id']);
      }
      
        if (body?.['account_link']) {
        query.andWhere("account_link", body?.['account_link']);
      }
      
        if (body?.['weixin_account']) {
        query.andWhere("weixin_account", body?.['weixin_account']);
      }
      
        if (body?.['fans_num']) {
        query.andWhere("fans_num", body?.['fans_num']);
      }
      
        if (body?.['evaluate_self']) {
        query.andWhere("evaluate_self", body?.['evaluate_self']);
      }
      
        if (body?.['evaluate_other']) {
        query.andWhere("evaluate_other", body?.['evaluate_other']);
      }
      
        if (body?.['progress']) {
        query.andWhere("progress", body?.['progress']);
      }
      
        if (body?.['province']) {
        query.andWhere("province", body?.['province']);
      }
      
        if (body?.['city']) {
        query.andWhere("city", body?.['city']);
      }
      
        if (body?.['ares']) {
        query.andWhere("ares", body?.['ares']);
      }
      
        if (body?.['address']) {
        query.andWhere("address", body?.['address']);
      }
      
        if (body?.['digger_time']) {
        query.andWhere("digger_time", body?.['digger_time']);
      }
      
        if (body?.['weixin_time']) {
        query.andWhere("weixin_time", body?.['weixin_time']);
      }
      
        if (body?.['last_follow_time']) {
        query.andWhere("last_follow_time", body?.['last_follow_time']);
      }
      
        if (body?.['check_apply_time']) {
        query.andWhere("check_apply_time", body?.['check_apply_time']);
      }
      
        if (body?.['check_apply_user']) {
        query.andWhere("check_apply_user", body?.['check_apply_user']);
      }
      
        if (body?.['check_apply_user_id']) {
        query.andWhere("check_apply_user_id", body?.['check_apply_user_id']);
      }
      
        if (body?.['face_time']) {
        query.andWhere("face_time", body?.['face_time']);
      }
      
        if (body?.['contract_time']) {
        query.andWhere("contract_time", body?.['contract_time']);
      }
      
        if (body?.['over_time']) {
        query.andWhere("over_time", body?.['over_time']);
      }
      
        if (body?.['expire_time']) {
        query.andWhere("expire_time", body?.['expire_time']);
      }
      
        if (body?.['over_reson']) {
        query.andWhere("over_reson", body?.['over_reson']);
      }
      
        if (body?.['import_level']) {
        query.andWhere("import_level", body?.['import_level']);
      }
      
        if (body?.['level_next_time']) {
        query.andWhere("level_next_time", body?.['level_next_time']);
      }
      
        if (body?.['top_state']) {
        query.andWhere("top_state", body?.['top_state']);
      }
      
        if (body?.['face_state']) {
        query.andWhere("face_state", body?.['face_state']);
      }
      
        if (body?.['exploit_state']) {
        query.andWhere("exploit_state", body?.['exploit_state']);
      }
      
        if (body?.['star_celebrity']) {
        query.andWhere("star_celebrity", body?.['star_celebrity']);
      }
      
        if (body?.['state']) {
        query.andWhere("state", body?.['state']);
      }
      
        if (body?.['tenant_type']) {
        query.andWhere("tenant_type", body?.['tenant_type']);
      }
      
        if (body?.['tenant_id']) {
        query.andWhere("tenant_id", body?.['tenant_id']);
      }
      
        if (body?.['del_state']) {
        query.andWhere("del_state", body?.['del_state']);
      }
      
        if (body?.['add_user']) {
        query.andWhere("add_user", body?.['add_user']);
      }
      
        if (body?.['add_user_id']) {
        query.andWhere("add_user_id", body?.['add_user_id']);
      }
      
        if (body?.['add_time']) {
        query.andWhere("add_time", body?.['add_time']);
      }
      
        if (body?.['edit_user']) {
        query.andWhere("edit_user", body?.['edit_user']);
      }
      
        if (body?.['edit_user_id']) {
        query.andWhere("edit_user_id", body?.['edit_user_id']);
      }
      
        if (body?.['last_edit_time']) {
        query.andWhere("last_edit_time", body?.['last_edit_time']);
      }
      
        if (body?.['server_number']) {
        query.andWhere("server_number", body?.['server_number']);
      }
      


      if (ispage) {
            if (body?.pageSize) {
              query.limit(body?.pageSize)
            }


var pageNum=body?.pageNum ||1
            if (pageNum) {
              query.offset( (pageNum-1 )* body?.pageSize )
            }
      }



  }

  getDb() {
    return this.app['erpdb']
  }



  async getlist(body) {
    console.log("开始 列表 ");

    var query = this.getDb().select().from("erp_cyberstar");
    var query1 = this.getDb().select().from("erp_cyberstar");

    //获取默认字段
    this.querybuildcomon(query, body);
    this.querybuildcomon(query1, body, false);
    var totalresults = await query1.count('id')
    var total = totalresults?.[0]?.['count(`id`)'];
    console.log('total :', total);

    query.orderBy('id','desc')

    return okres(await query, { total });
  }

  async getbyitem(body) {
    console.log("开始 获取单个 id:", body?.id);
    var query = this.getDb().select("*").from("erp_cyberstar");
    console.log("body :", body);


    if (!body?.id) {
      return okres(null);
    }

    if ((!body) || !Object.keys(body).length) {
      return okres(null);
    }

    if (body?.id) {
      query.andWhere("id", '=', body?.id);
    } else {

    }


    console.log("query :", query);

    var lists = await query;
    var one = lists?.[0];
    console.log('开始sq沙溪村  :', one);

    return okres(one);
  }

  async 删除(body) {
    if (!body.id) {
      return failres('没有id')
    }
    var delresult = await this.getDb()('erp_cyberstar').where('id', body.id).del();
    return okres(delresult);
  }

  async addorupdate(body) {
    console.log("开始 新增or修改 ");
    var rsiltonme = await this.getbyitem(body);
    var one = rsiltonme && rsiltonme.data;
    console.log("one :", one);

    var result = undefined;
    if (one?.id) {
      var __params = cloneDeep(body)
      delete __params.id
      result = await this.getDb()("erp_cyberstar").where("id", '=', one.id).update(__params);
    } else {
      // 新增
      result = await this.getDb()("erp_cyberstar").insert(body);
    }
    return okres(result);
  }

}